
install.packages("lmtest")
install.packages("margins")
install.packages("modelsummary")
install.packages("gt")


library(stargazer)
library(sandwich)
library(lmtest)
library(margins)
library(modelsummary)
library(broom)
library(gt)

# Import data
here::i_am("replication_dishonesty_infectious_diseases")
data <- haven::read_dta(here::here("1_Data", "master_data.dta"))


# Regressions:

models <- list (
  "(1)" = lm(ever_positive ~ ten_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data),
  "(2)" = margins(glm(ever_positive ~ ten_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data, family = binomial)),
  "(3)" = lm(ever_positive ~ likelihood_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data),
  "(4)" = margins(glm(ever_positive ~ likelihood_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data, family = binomial))
  )
modelsummary(models)

  coef_map <- c(
    "ten_tails_w1" = "Reported ten tails", 
    "likelihood_tails_w1" = "Likelihood of coin toss", 
    "age" = "Age in years", 
    "female" = "Female (1=yes, 0=no)",
    "educ_w1" = "Education",
    "hh_inc_w1" = "Household income", 
    "hh_18" = "Household members > 18 years", 
    "ff_60" = "No. of family members and friends > 60 years",
    "health_issue_w1" = "Health issue (1=yes, 0=no)", 
    "protect_others" = "Protective measures for others", 
    "perception_gov_reg" = "Perception government regulation", 
    "will_fin_risk" = "Willingness to take financial risks"
  )


stars_custom <- c("*" = 0.1, "**" = 0.05, "***" = 0.01)
  
  
model_table <- modelsummary(models, 
                            coef_map = coef_map, 
                            stars = stars_custom, 
                            output = "gt")

gt::gtsave(model_table, "3_Results/Table_A2.html")

gt::gtsave(model_table, "3_Results/Table_A2.tex")







########### does not work:

#Reported ten tails
# OLS
model1 <- lm(ever_positive ~ ten_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data)
summary(model1)
robust_se1 <- vcovHC(model1, type = "HC1")
robust_results1 <- coeftest(model1, vcov = robust_se1)
print(robust_results1)

#Logit
model1a <- glm(ever_positive ~ ten_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data, family = binomial)
summary(model1a)
#marginal effects:
marginal_effects1a <- margins(model1a, type = "response", se = TRUE)
summary(marginal_effects1a)

# Likelihood of coin toss
model2 <- lm(ever_positive ~ likelihood_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data)
summary(model2)
robust_se2 <- vcovHC(model2, type = "HC1")
robust_results2 <- coeftest(model2, vcov = robust_se2)
summary(robust_results2)

#Logit
model2a <- glm(ever_positive ~ likelihood_tails_w1 + age + female + educ_w1 + hh_inc_w1 +hh_18 +ff_60 +health_issue_w1+ protect_others + perception_gov_reg +will_fin_risk, data = data, family = binomial)
summary(model2a)
#marginal effects:
marginal_effects2a <- margins(model2a, type = "response", se = TRUE)
summary(marginal_effects2a)


stargazer(robust_results1, robust_results2, marginal_effects1a, marginal_effects2a, type = "html",
          align = TRUE, # Align coefficients and standard errors
          ci = TRUE, # Display confidence intervals
      title = "Covid-19 infection, ten teil tosses and likelihood of reported tails.",
      covariate.labels = c("Reported ten tails", "Likelihood of coin toss", "Age in years", "Female (1=yes, 0=no)",
                           "Education", "Household income", "Household members > 18 years", "No. of family members and friends > 60 years",
                           "Health issue (1=yes, 0=no)", "Protective measures for others", "Perception government regulation", 
                           "Willingness to take financial risks"),
      out = "3_Results/Table_A2.html") 

stargazer(robust_results1, robust_results2, marginal_effects1a, marginal_effects2a, type = "latex",
          align = TRUE, # Align coefficients and standard errors
          ci = TRUE, # Display confidence intervals
          title = "Covid-19 infection, ten teil tosses and likelihood of reported tails.",
          covariate.labels = c("Reported ten tails", "Likelihood of coin toss", "Age in years", "Female (1=yes, 0=no)",
                               "Education", "Household income", "Household members > 18 years", "No. of family members and friends > 60 years",
                               "Health issue (1=yes, 0=no)", "Protective measures for others", "Perception government regulation", 
                               "Willingness to take financial risks"),
          out = "3_Results/Table_A2.tex") 






